package com.leetcode.hot100;

import java.util.Arrays;

/**
 * @BelongsProject: LeetCode
 * @BelongsPackage: com.leetcode.hot100
 * @Author: lei
 * @CreateTime: 2024-03-29  18:40
 * @Description: TODO
 * @Version: 1.0
 */
public class 缺失的第一个正数 {
    public static void main(String[] args) {
        int[] nums = {-10, -3, -100, -1000, -239, 1};
        //int[] nums = {3, 4, -1, 1};
        //int[] nums = {1, 2, 0};
        System.out.println(firstMissingPositive(nums));
    }

    public static int firstMissingPositive(int[] nums) {
        int result = 1;
        int index = 0;
        boolean flag = false;
        Arrays.sort(nums);
        while (index < nums.length) {
            int temp;
            if (index == 0) {
                temp = nums[0];
                if (temp < 0) {
                    index++;
                    continue;
                } else if (temp > result) {
                    break;
                }
            } else {
                temp = nums[index - 1];
            }
            int current = nums[index];
            if (temp < 0) {
                index++;
                continue;
            } else if (temp == 1 || current == 1) {
                flag = true;
            }

            if (current > temp + 1) {
                result = temp + 1;
                break;
            } else if (index == nums.length - 1) {
                result = nums[nums.length - 1] + 1;
                index++;
            } else {
                index++;
            }
        }
        return flag ? result : 1;
    }
}
